﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraTab;

namespace Medicalsystem.MCP.FormDesigner.Ctl
{
    public partial class ctl_TabMenu : DevExpress.XtraEditors.XtraUserControl
    {
        public ctl_TabMenu()
        {
            InitializeComponent();
        }

        public delegate void ClickHandler(object sender, ModelShowEventArgs e);

        /// <summary>
        /// 选项卡改变事件
        /// </summary>
        public event ClickHandler TabChangedEvent;

        private bool _isAdd = false;

        /// <summary>
        /// 选项卡
        /// </summary>
        public DevExpress.XtraTab.XtraTabControl myTabControl
        {
            get { return this.tabControlMenu; }
        }

        /// <summary>
        /// 选取卡片显示文本
        /// </summary>
        public string SelectedTabText
        {
            get { return this.tabControlMenu.SelectedTabPage.Text; }
            set { this.tabControlMenu.SelectedTabPage.Text = value; }
        }

        /// <summary>
        /// 初始化，默认选项卡
        /// </summary>
        /// <param name="Name"></param>
        /// <param name="Text"></param>
        public void InitMyTabMenu(string Name, string Text)
        {
            this.tabControlMenu.SelectedTabPage.Name = Name;
            this.tabControlMenu.SelectedTabPage.Text = Text;
        }

        /// <summary>
        /// 添加选项页
        /// </summary>
        /// <param name="Name"></param>
        /// <param name="Text"></param>
        public string AddTabMenu(string Name, string Text)
        {
            //处理过程
            //1、检查当前Name的选项卡是否已经添加
            //2、是-将选项卡选中 否-添加当前选项卡并选中
            //3、如果有上一选项卡与当前选项卡不同 返回上一选项卡Name

            if (!_isAdd)
            {
                this.tabControlMenu.SelectedTabPage.Name = Name;
                this.tabControlMenu.SelectedTabPage.Text = Text;
                _isAdd = true;

                return Name;
            }

            string preName = this.tabControlMenu.SelectedTabPage.Name;
            bool alreadyAdd = false;

            foreach (XtraTabPage Tp in this.tabControlMenu.TabPages)
            {
                if (Tp.Name.Equals(Name))
                {
                    this.tabControlMenu.SelectedTabPage = Tp;
                    alreadyAdd = true;
                    break;
                }
            }

            if (alreadyAdd)
            {
                if (preName.Equals(Name))
                {
                    preName = null;
                }
            }
            else
            {
                DevExpress.XtraTab.XtraTabPage NewTP = new DevExpress.XtraTab.XtraTabPage();
                NewTP.Name = Name;
                NewTP.Text = Text;

                this.tabControlMenu.TabPages.Add(NewTP);
                this.tabControlMenu.SelectedTabPage=NewTP;
            }

            

            return preName;
        }

        private void OnTabChangedEvent(ModelShowEventArgs e)
        {
            if (TabChangedEvent != null)
            {
                TabChangedEvent(this, e);
            }
        }



        private void tabControlMenu_SelectedPageChanged(object sender, TabPageChangedEventArgs e)
        {
            ModelShowEventArgs TabMenuArgs = new ModelShowEventArgs(this.tabControlMenu.SelectedTabPage.Name, this.tabControlMenu.SelectedTabPage.Text);
            //通知外部选项卡改变
            OnTabChangedEvent(TabMenuArgs);
        }

    }
}
